package com.etc.servlet;

import com.etc.dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author kalista
 * @Description
 * @Date 2020/12/11  16:19
 **/
@WebServlet("/update")
public class UpdateBookServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String pice = req.getParameter("pice");
        String status = req.getParameter("status");

        BookDao bookDao = new BookDao();
        try {
            int update = bookDao.update(id, name, pice, status);
            if (update > 0){
                req.getRequestDispatcher("/index").forward(req,resp);
            }else{
                req.setAttribute("message","更新失败");
                req.getRequestDispatcher("/errorservlet").forward(req,resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            req.setAttribute("message","更新失败");
            req.getRequestDispatcher("/errorservlet").forward(req,resp);
        }


    }
}
